package com.esp.soccertutor.ui.xmlparser;

import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.esp.soccertutor.ui.bean.BeanCountry;
import com.esp.soccertutor.util.Log;


public class XMLParser_Country {
	
	public static String TAG = XMLParser_Country.class.getCanonicalName();
	private String strServerXML;
	public static Document docCountry;
	
	public static int STATUS_CODE;
	public static int STATUS_VALID_CID = -1;
	public static String MESSAGE;
	
	private ArrayList<BeanCountry> arrylstBeanCountry;
	
	
	public XMLParser_Country(String wsURL){
		
		try
		{
			XMLDocument.getHttpGetXML(wsURL);
			strServerXML = "<response><code>1</code><message> Ok</message><countries><country><countryid>1</countryid><countryname>United Kingdom</countryname>"
					+ "</country><country><countryid>2</countryid><countryname>USA</countryname></country><country><countryid>3</countryid><countryname>Canada</countryname></country>"
					+ "</countries></response>";

			if(XMLDocument.HTTP_XML_STATUS == 200)
			{
				Log.print(TAG,strServerXML);
				docCountry = XMLDocument.XMLfromString(strServerXML.replaceAll("&","&amp;").trim());
				STATUS_CODE = 200;
				
			}
			else if(XMLDocument.HTTP_XML_STATUS == 401)
			{
				// XML not validating 
				STATUS_CODE = 401;
			}
			else if(XMLDocument.HTTP_XML_STATUS == 500)
			{
				// 	internal error occurred								
				STATUS_CODE = 500;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			STATUS_CODE = 500;
		}
	}
	
	/**
	 * THIS CALLBACK WHEN WE REACHED DOCUMENT
	 * @return
	 */
	public static Document GetDocument(){
		
		if(docCountry != null){
			return docCountry;
		}else{
			return null;
		}
	}
	

	public ArrayList<BeanCountry> getCountry(Document docCountry){
		try
		{
			if(docCountry != null)
				return docBuild(docCountry);
			else
				return null;
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	
	private ArrayList<BeanCountry> docBuild(Document doc) throws Exception{
		
		if(doc != null)
		{
			Element elem$root = doc.getDocumentElement();
			NodeList nl$item = elem$root.getElementsByTagName("code");
			Element  line  = (Element)nl$item.item(0);
			STATUS_VALID_CID = Integer.parseInt(XMLDocument.getCharacterDataFromElement(line, "code"));
			
			NodeList nodeMessage = elem$root.getElementsByTagName("message");
			line  = (Element)nodeMessage.item(0);
			MESSAGE = XMLDocument.getCharacterDataFromElement(line, "message");
			
			NodeList nodeCountry = elem$root.getElementsByTagName("country");
			
			if(STATUS_VALID_CID == 1)
			{
				arrylstBeanCountry = new ArrayList<BeanCountry>();
				
				for (int i = 0; i < nodeCountry.getLength(); i++) 
					{
						
						BeanCountry mBeanCountry = new BeanCountry();
						
						Element elementPos= (Element) nodeCountry.item(i);
						
						NodeList nodeID = elementPos.getElementsByTagName("countryid");
						line  = (Element)nodeID.item(0);
						mBeanCountry.setCountryid(XMLDocument.getCharacterDataFromElement(line, "countryid"));
						
						
						NodeList nodeName = elementPos.getElementsByTagName("countryname");
						line  = (Element)nodeName.item(0);
						mBeanCountry.setCountryname(XMLDocument.getCharacterDataFromElement(line, "countryname"));
						
						arrylstBeanCountry.add(mBeanCountry);
					
					}
			}
			
		}
		return arrylstBeanCountry;
	}
	
	
	
}
